
const $common = require('../../common/common-req.js');
const router = $common.express.Router();
const requestHandler = require('../../common/request-handler.js');

/**
 * 添加用户关联项目
 * */
 router.post('/', (req, res) => {
     const createTime = new Date();
     const updateTime = new Date();
     let param = ['phone', 'projectId', 'identity'];
     let vital = ['phone', 'projectId', 'identity'];
     let expand = {createTime, updateTime};
     let insertSql = 'insert into project_user(phone, projectId, identity, createTime, updateTime) values(?, ?, ?, ?, ?)';
     let insertArr = ['phone', 'projectId', 'identity', 'createTime', 'updateTime'];
     let moduleName = '用户关联项目';
     let isExitArr = ['phone', 'projectId'];
     let exitTableName = 'project_user';
     requestHandler.postHandler(req, res, param, vital, expand, insertSql, insertArr, moduleName, isExitArr, exitTableName);
 })
 
 /**
  * 获取项目关联用户列表
  * */
 router.get('/', (req, res) => {
     let param = ['pageNum', 'pageSize', 'projectId'];
     let vital = ['pageNum', 'pageSize', 'projectId'];
     let totalSql = 'select count(*) as count from project_user where projectId=?';
     let expand = [];
     let getSql = 'select * from project_user_view where projectId=? order by updateTime limit ?, ?';
     let getArr = ['projectId'];
     let selectAttr = ['phone', 'userName', 'projectId', 'identity', 'status', 'createTime', 'updateTime'];
     let reName = ['phone', 'userName', 'projectId', 'identity', 'status', 'createTime', 'updateTime'];
     let moduleName = '用户关联项目列表';
     requestHandler.getHandler(req, res, param, vital, totalSql, expand, getSql, getArr, selectAttr, reName, moduleName );
 })
 
// 获取用户拥有管理员权限的项目
router.get('/getProjectIsManage', (req, res) => {
    let param = ['pageNum', 'pageSize', 'phone'];
    let vital = ['pageNum', 'pageSize', 'phone'];
    let totalSql = 'select count(*) as count from project_user_view where phone=? and (identity=2 or identity=3)';
    let expand = [];
    let getSql = 'select * from project_user_view where phone=? and (identity=2 or identity=3) order by updateTime limit ?, ?';
    let getArr = ['phone'];
    let selectAttr = ['projectId', 'projectName', 'phone', 'identity', 'status', 'createTime', 'updateTime'];
    let reName = ['projectId', 'projectName', 'author', 'identity', 'status', 'createTime', 'updateTime'];
    let moduleName = '用户拥有管理员权限的项目列表';

    requestHandler.getHandler(req, res, param, vital, totalSql, expand, getSql, getArr, selectAttr, reName, moduleName, false, [], async (result) => {
        console.log('result', result)
        let getProject = 'select * from project_view where author=? ';
        let getProjectArr = [req.query.phone];
        let _res = await $common.db_mysql.asyncSelect(getProject, getProjectArr)
        _res = _res.map(item => {
            let _t = {
                projectId: item.projectId,
                projectName: item.projectName,
                author: item.author,
                identity: '4',
                status: item.status,
                createTime: item.createTime,
                updateTime: item.updateTime
            }
            return _t
        })
        result.total += _res.length
        result.list = [..._res, ...result.list]
        return result
    });
})
 
 // 获取用户拥有普通用户身份的项目
router.get('/getProjectIsGeneral', (req, res) => {
     let param = ['pageNum', 'pageSize', 'phone', 'projectId'];
     let vital = ['pageNum', 'pageSize', 'phone', 'projectId'];
     let totalSql = 'select count(*) as count from project_user_view where phone=? and projectId=? and identity=1';
     let expand = [];
     let getSql = 'select * from project_user_view where phone=? and projectId=? and identity=1 order by updateTime limit ?, ?';
     let getArr = ['phone', 'projectId'];
     let selectAttr = ['projectId', 'projectName', 'phone', 'identity', 'status', 'createTime', 'updateTime'];
     let reName = ['projectId', 'projectName', 'phone', 'identity', 'status', 'createTime', 'updateTime'];
     let moduleName = '用户拥有普通用户权限的项目列表';
     requestHandler.getHandler(req, res, param, vital, totalSql, expand, getSql, getArr, selectAttr, reName, moduleName);
})
 
 /**
  * 修改用户关联项目状态
  * */
 router.put('/', (req, res) => {
     const updateTime = new Date();
     let param = ['projectId', 'phone', 'identity'];
     let vital = ['projectId', 'phone'];
     let expand = { updateTime };
     let updateSql = 'update project_user set identity=?, updateTime=? where projectId=? and phone=?';
     let updateArr = ['identity', 'updateTime', 'projectId', 'phone'];
     let moduleName = '用户关联项目状态';
     requestHandler.putHandler(req, res, param, vital, expand, updateSql, updateArr, moduleName);
 })
 
 /**
  * 删除用户关联项目信息
  * */
 router.delete('/', (req, res) => {
     let param = ['projectId', 'phone'];
     let vital = ['projectId', 'phone'];
     let expand = [];
     let delSql = 'delete from project_user where projectId=? and phone = ?';
     let delArr = ['projectId', 'phone'];
     let moduleName = '用户关联项目信息';
     requestHandler.delHandler(req, res, param, vital, expand, delSql, delArr, moduleName);
 })
 
 module.exports = router